import { createFormData, getFormDetail, updateFormData } from "@/api/form";

export default {
    data() {
        return {
            createModalWidth: 700,
            createVisible: false,
            modalLoading: true,
            modalTitle: "",
            maskClosable: false,
            editInfo: {}
        }
    },
    created() {

    },
    methods: {
        onFormSave() {
            try {
                this.$refs.create.handleSubmit(
                    result => {
                        try {
                            const formData = Object.assign({}, result);

                            if (formData.id) {
                                updateFormData(this.formId, formData)
                                    .then(result => {
                                        this.$Message.success("编辑成功");
                                        this.refreshDate();
                                    })
                                    .catch(err => {
                                        this.resetModal();
                                    });
                            } else {
                                createFormData(this.formId, formData)
                                    .then(result => {
                                        this.$Message.success("添加成功");
                                        this.refreshDate();
                                    })
                                    .catch(err => {
                                        this.resetModal();
                                    });
                            }
                        } catch (error) {
                            console.log(error)
                        }

                    },
                    () => {
                        this.resetModal();
                    }
                );
            } catch (error) {
                console.log(error)
            }

        },
        refreshDate() {
            this.modalLoading = false;
            this.$nextTick(() => {
                this.modalLoading = true;
                this.createVisible = false;
                this.$refs.create.handleReset();

                this.$refs.table.refreshDate();
            });
        },
        handleOpenCreate() {
            this.modalTitle = `添加${this.title}`;
            this.createVisible = true;
            this.editInfo = {};
        },
        resetModal() {
            this.modalLoading = false;
            this.$nextTick(() => {
                this.modalLoading = true;
            });
        },
        handleDelete() {
            this.$refs.table.handleDelete();
        },
        handleOpenUpdate() {
            const table = this.$refs.table;
            const selectRow = table.$refs.tables.getSelection();

            if (selectRow.length === 0 || selectRow.length > 1) {
                this.$Message.error("请选择一行数据");
                return;
            }

            getFormDetail(this.formId, selectRow[0].id)
                .then(result => {
                    this.createVisible = true;
                    this.modalTitle = `编辑${this.title}`;
                    this.editInfo = result.data.data;
                })
                .catch(err => { });
        }
    }
};